//
//  HMInputView.swift
//  swift微博项目
//
//  Created by huodian on 16/10/8.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

let emojiKeyboardHeight:CGFloat = 220

let emojiBottomBarHeight:CGFloat = 37

let reuserId:String = "cellId"


class HMInputView: UIView {

   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setMyUI()
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setMyUI(){
        
        self.addSubview(toolbar)
        
        self.addSubview(collectionEmoji)
        
        toolbar.snp.makeConstraints { (make) in
            make.bottom.trailing.leading.equalTo(self)
            
            make.height.equalTo(emojiBottomBarHeight)
            
        }
        
        collectionEmoji.snp.makeConstraints { (make) in
            
            make.top.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(toolbar.snp.top)
        }
        
        
        toolbar.emojiBeselectedWithTheViewScroll = { type in
            
            let indexpath = IndexPath(item: 0, section: type.rawValue)
            //如果设置成true则在点击时,界面会挨个滑动,影响用户体验
            self.collectionEmoji.scrollToItem(at: indexpath, at: UICollectionViewScrollPosition.left, animated: false)
            
          
        }
        
        
    }
    //在一个界面中添加其他的View时,可以使用懒加载的方式去实例化,没有必要去在新建新的类来进行加载(一个类中可以创建不同的类)
    lazy var collectionEmoji:UICollectionView = {
       
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = 0
        
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .horizontal
        
        layout.itemSize = CGSize(width: screenWidth, height: emojiKeyboardHeight-emojiBottomBarHeight)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        
        
        
        collectionView.register(HMEmojiCell.self, forCellWithReuseIdentifier: reuserID)
        
        collectionView.dataSource = self
        
        collectionView.delegate = self
        
        collectionView.bounces = false
        
        collectionView.isPagingEnabled = true
        
   
        return collectionView
        
        
        
        
    }()
    
    lazy var toolbar:HMStackViewBar = HMStackViewBar()
  
}

extension HMInputView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 4
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuserID, for: indexPath) as! HMEmojiCell
        
        cell.indexpath  = indexPath
        
        cell.backgroundColor = UIColor.green
        
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offset = collectionEmoji.contentOffset.x + 0.5 * screenWidth
        
        let point  = CGPoint(x: offset, y: 1)
        
        let indexpath = collectionEmoji.indexPathForItem(at: point)
        
        toolbar.selectedEmojiView(indexpath: indexpath!)
        
        
    }
}
